********regressions*******
capture program drop plotreg
program plotreg
	
	local dv "`1'"
	local scale1 `2'
	local scale2 `3'
	local log `4'

	qui{
	forval q = 1/4{

		local controls `"cz_shock sex asian black hispan i.year i.state_2000#i.head_coll i.age_group head_mar head_constr_manu house_value parent_inc own_house"'
		reg `dv' c.cz_shock#i.age_group [aw = weight] if parent_inc_quart == `q', vce(cluster cz_2000)

		//storing estimates
		forval i = 1/4{
			local effect_`i'_`q' = round(_b[`i'.age_group#c.cz_shock], 0.001)
			local se_`i'_`q' = round(_se[`i'.age_group#c.cz_shock], 0.001)
			
			//rescaling income LHS variables
			if "`dv'" == "wag" | "`dv'" == "ti"{
				local effect_`i'_`q' = round(_b[`i'.age_group#c.cz_shock], 0.001) * 1000
				local se_`i'_`q' = round(_se[`i'.age_group#c.cz_shock], 0.001) * 1000
			}
			
			su `dv' [aw = weight] if age_group == `i' & parent_inc_quart == `q'
			local mean_`i'_`q' = round(`r(mean)', 0.01)
			
			//rescaling income LHS variables
			if "`dv'" == "wag" | "`dv'" == "ti"{
				local mean_`i'_`q' = round(`r(mean)', 0.01) * 1000
			}
		}
	}

	//create data for making graphs
	preserve
	clear
	set obs 16
	gen group = .
	gen effect = .
	gen se = .
	gen mean = .
	gen quart = .
	forval q = 1/4{
		forval i = 1/4{
			local line = (`q'-1)*4 + `i'
			replace quart = `q' in `line'
			replace group = `i' in `line'
			replace effect = `effect_`i'_`q'' in `line'
			replace se = `se_`i'_`q'' in `line'
			replace mean = `mean_`i'_`q'' in `line'
		}
	}
	}

	//upper, lower bounds
	gen ub = effect + 1.96*se
	gen lb = effect - 1.96*se
	local step1 = `scale1'/2
	local step2 = `scale2'/2

	//fancy graphs over quartiles
	forval q = 1/4{
		if `log' == 1{
			twoway connected effect group if quart == `q', graphregion(color(white)) bgcolor(white) ylabel(-`scale1'(`step1')`scale1', angle(0) grid) ///
			xlabel(1 "17-19" 2 "20-22" 3 "23-25" 4 "26-28") xtitle("Age Group") ytitle("Effect") lc(black) mc(black) ///
			|| line ub group if quart == `q', lp(dash) lc(black) || line lb group if quart == `q', lp(dash) lc(black) ///
			legend(order(1 "Effect")) yline(0) title("Parent Quartile `q'") saving(graph`q', replace)
		}
		else if `log'!=1{
			twoway connected effect group if quart == `q', graphregion(color(white)) bgcolor(white) ylabel(-`scale1'(`step1')`scale1', angle(0) grid) ///
			xlabel(1 "17-19" 2 "20-22" 3 "23-25" 4 "26-28") xtitle("Age Group") ytitle("Effect") lc(black) mc(black) ///
			|| line ub group if quart == `q', lp(dash) lc(black) || line lb group if quart == `q', lp(dash) lc(black) ///
			|| connected mean group if quart == `q', yaxis(2) ytitle("Basis", axis(2)) ylabel(-`scale2'(`step2')`scale2', axis(2) angle(0) grid) ms(T) mc(black) lc(black) ///
			legend(order(1 "Effect" 4 "Basis")) yline(0) title("Parent Quartile `q'") saving(graph`q', replace)
		}
	}
	graph combine graph1.gph graph2.gph graph3.gph graph4.gph, graphregion(color(white))
	graph export "$disc/4_effects_`dv'_quart.png", replace width(1500)

	//now report percentage effects over quartiles at final age group
	keep if group == 4	
	gen effect_pct = (effect/mean)*100
	gen se_pct = (se/mean)*100
	
	if `log'==1{
		replace effect_pct = effect
		replace se_pct = se
	}

	serrbar effect_pct se_pct quart, scale(1.96) bgcolor(white) graphregion(color(white)) ///
	ytitle("Percentage Effect, Age 26-28") xlabel(1 "1" 2 "2" 3 "3" 4 "4") xtitle("Parent Income Quartile") yline(0)
	graph export "$disc/4_pct_effects_`dv'_quart.png", replace width(1500)
	graph close
	restore
end


